﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



class RelevanceIndex
    {
        static void Main()
        {
            // 60 points BG Coder

            string searchWord = Console.ReadLine();
            int L = int.Parse(Console.ReadLine());
            string[] text = new string[L];

            searchWord = " " + searchWord + " ";


            for (int i = 0; i < L; i++)
            {
                text[i] = " " + Console.ReadLine() + " ";
            }

            Dictionary<string, int> dict = new Dictionary<string, int>();

            for (int i = 0; i < text.Length; i++)
            {
                text[i] = RemovePunctuation(text[i]);
                dict.Add(text[i], CountWordOccurrances(text[i], searchWord));

            }

            var sorted = dict.OrderByDescending(x => x.Value);

            foreach (var line in sorted)
            {
                string result = MakeSearchWordUpperCase(line.Key, searchWord);
                Console.WriteLine(result);
            }
        }

        static string MakeSearchWordUpperCase(string line, string searchWord)
        {
            string[] lineDevided = line.Trim().Split(' ');
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < lineDevided.Length; i++)
            {

                if (lineDevided[i].ToLower() == searchWord.Trim().ToLower())
                {
                    lineDevided[i] = lineDevided[i].ToUpper();

                }
                result.Append(lineDevided[i] + " ");
            }
            return result.ToString();

        }
    
    static string RemovePunctuation(string line)
        {
            foreach (char character in line)
            {
                if (char.IsPunctuation(character))
                {
                    if (character == '\'')
                    {
                        continue;
                    }
                    else if(character == '-')
                    {
                        line = line.Replace(character.ToString(), " ");
                    }
                    else
                    {
                        line = line.Replace(character.ToString(), "");
                    }
                    
                }

            }

            return line;
        }

static int CountWordOccurrances (string line, string searchWord)
        {
            
            string caseIncensitive = line.ToLower();
            int relevanceIndex = caseIncensitive.IndexOf(searchWord.ToLower());
            int counter = 0;
            
            while (relevanceIndex >= 0)
            {

                relevanceIndex = caseIncensitive.IndexOf(searchWord.ToLower(), relevanceIndex + 1);
                counter++;
            }

            return counter;

        }


}

